﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using mamnontuoitho7.AppCode;
using System.Text.RegularExpressions;

namespace mamnontuoitho7.module
{
    public partial class ParentConversation : System.Web.UI.Page
    {

        private Member _member = new Member();
        protected Parent _parent = new Parent();
        private Conversation _conversation = new Conversation();
        protected General _general = new General();

        protected DataTable _dt;
        protected int _classCount = 0;

        protected int _pageflag;
        protected int _currentbar = 1;

        protected int _totalPages;
        protected int _totalConversationNumber;
        protected int _totalBars;

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["memberID"].ToString() != "" && Session["role"].ToString() == "4")
                {

                    if (_parent.CheckParentAvailability(Session["memberID"].ToString()) == "2")
                    {
                        if (!IsPostBack)
                        {
                            // load drop down list article type
                            DataTable dtParentClass = _member.GetClassByParent(Session["memberID"].ToString());
                            _classCount = dtParentClass.Rows.Count;
                            ddlClass.DataSource = dtParentClass;
                            ddlClass.DataTextField = "ClassName";
                            ddlClass.DataValueField = "ClassID";
                            ddlClass.DataBind();
                        }

                        ////////////////////start paging
                        _totalConversationNumber = _conversation.TotalConversationOfOneMember(Session["memberID"].ToString()).GetValueOrDefault();
                        _totalPages = _general.Divide(_totalConversationNumber, ConstantClass._pageStepConversation);
                        _totalBars = _general.Divide(_totalPages, ConstantClass._barStepConversation);

                        if (Request.QueryString["page"] != null)// khi bấm vào các ô số
                        {
                            string temp = Request.QueryString["page"].ToString();
                            Match match = Regex.Match(temp, @"([0-9])$", RegexOptions.IgnoreCase);

                            if (temp.Length <= 4 && match.Success)
                            {
                                _pageflag = Convert.ToInt16(temp);// trang hiện hành
                                if (_pageflag <= _totalPages)
                                {
                                    _currentbar = _general.Divide(_pageflag, ConstantClass._barStepConversation);
                                    Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                                }
                                else
                                {
                                    _pageflag = 1;
                                    _currentbar = 1;
                                    Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                                }
                            }
                        }
                        if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
                        {
                            _currentbar = 1;
                            _pageflag = 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }

                        if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
                        {
                            _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                            if (_currentbar < _totalBars)
                            {
                                _currentbar += 1;
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                            _pageflag = (_currentbar - 1) * ConstantClass._barStepConversation + 1;
                        }

                        if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
                        {
                            _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                            if (_currentbar > 1)
                            {
                                _currentbar -= 1;
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                            _pageflag = (_currentbar - 1) * ConstantClass._barStepConversation + 1;
                        }

                        _dt = _conversation.GetConversationPaged(_pageflag, ConstantClass._pageStepConversation, Session["memberID"].ToString());
                        _beginPage = (_currentbar - 1) * ConstantClass._barStepConversation + 1;
                        _endPage = _currentbar * ConstantClass._barStepConversation;

                        if (_endPage > _totalPages)
                        {
                            _endPage = _totalPages;
                        }
                        //////////////////////////////end
                    }
                }
                else
                {
                    Response.Redirect(ConstantClass._errorLoginPage);
                }

                Session["Common_articleTypeID"] = "1";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void btnConversationSend_Click(object sender, EventArgs e)
        {
            try
            {
                _conversation.ConversationCreate(ddlClass.SelectedValue, txtConversation.Text.Trim(), Session["memberID"].ToString());
                txtConversation.Text = "";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorLoginPage);
            }

            Response.Redirect("ParentConversation.aspx");
        }
    }
}